package com.spring.boot;

import com.spring.context.ConfigurableApplicationContext;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author liyuncong
 * @version 1.0
 * @file SpringApplicationShutdownHook
 * @brief SpringApplicationShutdownHook
 * @details SpringApplicationShutdownHook
 * @date 2023-01-13
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2023-01-13                  liyuncong          Created
 */
public class SpringApplicationShutdownHook implements Runnable {

    private final Set<ConfigurableApplicationContext> contexts = new LinkedHashSet<>();

    private final Set<ConfigurableApplicationContext> closedContexts = new LinkedHashSet<>();

    private final AtomicBoolean shutdownHookAdded = new AtomicBoolean();

    void registerApplicationContext(ConfigurableApplicationContext context) {

    }

    private void addRuntimeShutdownHookIfNecessary() {
        if (this.shutdownHookAdded.compareAndSet(false, true)) {
            addRuntimeShutDownHook();
        }
    }

    void addRuntimeShutDownHook() {
        try {
            Runtime.getRuntime().addShutdownHook(new Thread(this, "SpringApplicationShutdownHook"));
        } catch (Exception exception) {
            // Not allowed in some environments
        }
    }


    @Override
    public void run() {

    }
}
